﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.Reflection;
using Cn.Cellula.Biz;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Product {
    public partial class AddBaseInfo : PageSupport {
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        ProductEditBiz peb = new ProductEditBiz();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
            }
        }
        private void InitForm() {
            ClientBiz.BindCodeList(ref this.ddl_Material, CodeTypeDef.Material.Key, true, null);
            ClientBiz.BindCodeList(ref this.ddl_Unit, CodeTypeDef.ProductUnit.Key, true, null);

            if (SiteConfig.Product.ProductNoAuto == true) {
                this.txt_ProductNo.Attributes.Add("readonly", "readonly");
                this.txt_ProductNo.CssClass = "input_readonly";
                this.txt_ProductNo.Text = biz.GenerateProductNo();
            }
            ProductActor pa = peb.PopFlow(Session);
            if (pa != null) {
                this.lab_CategoryName.Text = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, CodeTypeDef.Category.Product, pa.CategoryKey," / ");
                this.hid_CategoryKey.Value = pa.CategoryKey;
            } else {
                SiteInfo.Send(Page, "错误", "没有选择有效的产品分类");
                this.btn_save.Enabled = false;
                return;
            }

        }

        private ProductActor DoSave() {
            Boolean vd = QuickValidator.Execute(Request, this.txt_ProductName.Parent);
            if (!vd) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return null;
            }

            TProductEO eo = new TProductEO();
            if (biz.IfProductNoExist(this.txt_ProductNo.Text)) {
                SiteInfo.Send(Page, TxtDef.ERROR, "产品编号已经存在，请重新提交");
                this.txt_ProductNo.Text = biz.GenerateProductNo();
                return null;
            } else if (String.IsNullOrEmpty(this.txt_ProductNo.Text)) {
                SiteInfo.Send(Page, TxtDef.ERROR, "请输入产品编号");
                this.txt_ProductNo.Text = biz.GenerateProductNo();
                return null;
            } else {
                eo.ProductNo = this.txt_ProductNo.Text;
            }
            eo.OwnerKey = this.OwnerKey;
            eo.BarCode = "";
            eo.CategoryId = this.hid_CategoryKey.Value;
            eo.Color = this.txt_Color.Text;
            eo.Description = this.mtxt_Desc.Text;
            eo.FactoryDate = CtrlUtil.ParseDateTime(this.txt_FactoryDate.Text);
            eo.Height = CtrlUtil.ParseDecimal(this.txt_Height.Text);
            eo.IsColorable = (this.cbx_Colorable.Checked) ? 1 : 0;
            eo.IsSizable = (this.cbx_Sizable.Checked) ? 1 : 0;
            eo.IsVerified = 1;
            eo.Length = CtrlUtil.ParseDecimal(this.txt_Length.Text);
            eo.Manufacturer = this.txt_Manufacturer.Text;
            eo.MaterialId = this.ddl_Material.SelectedValue;
            eo.ProducingArea = this.txt_ProducingArea.Value;
            eo.ProductDate = CtrlUtil.ParseDateTime(this.txt_ProductDate.Text);
            eo.ProductModel = this.txt_Model.Text;
            eo.ProductName = this.txt_ProductName.Text;
            eo.Volume = CtrlUtil.ParseDecimal(this.txt_Volume.Text);
            eo.Weight = CtrlUtil.ParseDecimal(this.txt_Weight.Text);
            eo.Width = CtrlUtil.ParseDecimal(this.txt_Width.Text);
            eo.Tags = this.txt_Tags.Text;
            eo.UnitKey = this.ddl_Unit.SelectedValue;
            eo.Attachment = this.txt_Attechment.Text;
            try {
                eo.ClickCount = Convert.ToInt32(this.txt_ClickCount.Text);
            } catch { }

            eo.CreateDate = eo.LastDate = DateTime.Now;
            eo.CreateOperator = eo.UpdateOperator = (this.Actor != null) ? this.Actor.ID : "unkown";
            String key = (String)biz.Save<TProductEO>(eo);
            BizFactory.Get<ILogBiz>().LogInsert("产品", eo.ProductName, this.Actor, Request);

            ProductActor pa = new ProductActor();
            pa.CategoryKey = this.hid_CategoryKey.Value;
            pa.ProductKey = key;
            return pa;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            try {
                ProductActor pa = DoSave();
                if (pa == null) return;
                peb.PushFlow(Session, pa);
                peb.GoNext(Request, Response);
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, "错误", "添加商品失败");
                return;
            }
        }

        protected void btn_Prevouse_Click(object sender, EventArgs e) {
            peb.GoPrevious(Request,Response);
        }

        protected void btn_Finish_Click(object sender, EventArgs e) {
            try {
                ProductActor pa = DoSave();
                if (pa == null) return;
                peb.Finish(Response);
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, "错误", "添加商品失败");
                return;
            }
        }
    }
}
